package com.wordpress.minhnvt1;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.wordpress.minhnvt1.hrm.dba.DBAdapter;

public class MainActivity extends Activity {
    
	Button btnReset;
	Button btnLogin;
	Button btnRegister;
	
	EditText txtName;
	EditText txtPass;
	
	private DBAdapter _db;
	private String _hrId;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        btnReset = (Button)findViewById(R.id.btnMainReset);
        btnLogin = (Button)findViewById(R.id.btnMainLogin);
        btnRegister = (Button)findViewById(R.id.btnMainRegister);
        
        txtName = (EditText)findViewById(R.id.etMainName);
        txtPass = (EditText)findViewById(R.id.etMainPass);
        
        btnReset.setOnClickListener(new OnClickListener() {
			
        	public void onClick(View v) {
				txtName.setText("");
				txtPass.setText("");
			}
		});
        
        btnRegister.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), RegisterForm.class);
				startActivity(intent);
			}
		});
        btnLogin.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				_db = new DBAdapter(getApplicationContext());
				_db.openDB();
				
				String name = txtName.getText().toString();
				String pass = txtPass.getText().toString();
				
				if (_db.checkLogin(name, pass) == true) {
					Cursor c = _db.getHumanResourceByName(name);
					_hrId = c.getString(0);
					
					Intent intent = new Intent(getApplicationContext(), ManagementForm.class);
					intent.putExtra("hrId", _hrId);
					
					startActivity(intent);
					_db.closeDB();
				} else if (_db.checkLogin(name, pass) == false) {
					Toast.makeText(getApplicationContext(), "Login failed, try again!.", Toast.LENGTH_SHORT).show();
					_db.closeDB();
				}
			}
		});
    }
}